package models

import (
	"WeChatwebversion/utils"
	"fmt"
	"io"
	"net/http"
	"os"

	"github.com/eatmoreapple/openwechat"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
)


var Url string
func ConsoleQrCode(uuid string) {
	Url="https://login.weixin.qq.com/qrcode/"+uuid
}
func WeChatLogin(claims utils.Claims,this *gin.Context) {
	bot := openwechat.DefaultBot(openwechat.Desktop)
	bot.UUIDCallback = ConsoleQrCode
	// 发送 HTTP GET 请求
	response, err := http.Get(Url)
	if err != nil {
		fmt.Println("Failed to download image:", err)
		utils.SendMessage(2,200,"服务器繁忙！","",this)
		return
	}
	defer response.Body.Close()

	// 创建本地文件
	file, err := os.Create(claims.Username+".jpg")
	if err != nil {
		fmt.Println("Failed to create file:", err)
		return
	}

	// 将图像内容复制到本地文件
	_, err = io.Copy(file, response.Body)
	if err != nil {
		fmt.Println("Failed to save image:", err)
		return
	}
	file.Close()
	socket:=utils.ClientsSocket[claims.Username]
	socket.WriteMessage(websocket.TextMessage,[]byte("http://localhost:8088/static/"+claims.Username+".jpg"))
}